#include "Electric_Brain.h"
#include <math.h>

GLfloat ebrain_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat ebrain_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat ebrain_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat ebrain_shininess = 10.0f;

Electric_Brain::Electric_Brain()
{
	// UPGRADES
	upgradeLife = 5;
	upgradePower = 1;

	heightBaseEB = 0.5;
	heightAntennaEB = 1.0;
	radiusBallEB = 0.1;
	quad = gluNewQuadric();
	loadImage();
}

int Electric_Brain::getUpgradeLife()
{
	return upgradeLife;
}

int Electric_Brain::getUpgradePower()
{
	return upgradePower;
}

void Electric_Brain::loadImage()
{
	Image* image1 = loadBMP("textures\\textureAntigrav.bmp");
	Image* image2 = loadBMP("textures\\textureAntigrav2.bmp");
	Image* image3 = loadBMP("textures\\alu.bmp");
	Image* image4 = loadBMP("textures\\copper.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureAntenna1 = loadTexture(image3);
	textureAntenna2 = loadTexture(image4);

	currentTextureBase = textureBase1;
	currentTextureAntenna = textureAntenna1;

	delete image1;
	delete image2;
	delete image3;
	delete image4;
}

void Electric_Brain::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureAntenna == textureAntenna1)
		currentTextureAntenna = textureAntenna2;
	else
		currentTextureAntenna = textureAntenna1;

}

// This function is called to display the .
void Electric_Brain::display()
{
	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, ebrain_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ebrain_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ebrain_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, ebrain_shininess);

		glColor3f(1,1,1);
		glTranslatef(0, (heightBaseEB/2), 0);
		
		// Cube Base
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, currentTextureBase);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glScalef(1.0, heightBaseEB, 1.0);
			drawCube(0.5, 0.75);
		glPopMatrix();
		
		// Antenna Cone
		glPushMatrix();
			gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, currentTextureAntenna);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

			glTranslatef(0.0, 0.2, 0.0);
			glRotatef(-90.0, 1.0, 0.0, 0.0);
			gluCylinder(quad, 0.1, 0.0, heightAntennaEB, 10.0, 10.0);
		glPopMatrix();

		// Top Sphere of Antenna
		glPushMatrix();
			glTranslatef(0.0, 1.2, 0.0);
			gluSphere(quad, radiusBallEB, 10.0, 5.0);
		glPopMatrix();

		glDisable(GL_CULL_FACE);
		//DISKS
		glPushMatrix();
			glTranslatef(0.0, 0.2, 0.0);
			glPushMatrix();
				glTranslatef(0.0, 0.1, 0.0);
				glRotatef(90.0, 1.0, 0.0, 0.0);
				gluDisk(quad, 0.15, 0.18, 20.0, 10.0);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0.0, 0.3, 0.0);
				glRotatef(90.0, 1.0, 0.0, 0.0);
				gluDisk(quad, 0.12, 0.15, 20.0, 10.0);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0.0, 0.5, 0.0);
				glRotatef(90.0, 1.0, 0.0, 0.0);
				gluDisk(quad, 0.09, 0.12, 20.0, 10.0);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0.0, 0.7, 0.0);
				glRotatef(90.0, 1.0, 0.0, 0.0);
				gluDisk(quad, 0.06, 0.09, 20.0, 10.0);
			glPopMatrix();
		glPopMatrix();
		glEnable(GL_CULL_FACE);
	glPopMatrix();
}

float Electric_Brain::getHeight()
{
	return heightBaseEB + heightAntennaEB + radiusBallEB;
}

Electric_Brain::~Electric_Brain(void)
{
	gluDeleteQuadric(quad);
}
